/* global md5, $, monster, ga, Intercom */

function showUser (user) {
  const hash = md5(user.details.email.toLowerCase())
  $('.gravatar').prop('src', '//www.gravatar.com/avatar/' + hash + '?s=32&ampd=mm')

  $('.userlink span').text(user.details.name)
  $('.signed-out').hide()
  $('.signed-in').show()
}

function bootIntercom (user) {
  let createdAt
  let email
  let account

  if (user && user.details) {
    createdAt = user.details.created_at
    createdAt = createdAt ? Math.round(createdAt / 1000) : undefined
    createdAt = isNaN(createdAt) ? undefined : createdAt

    email = user.details.email
    account = user.account
  }

  Intercom('boot', {
    app_id: '<%= intercom_id %>',
    email: email,
    user_id: account,
    created_at: createdAt,
    widget: {
      activator: '#IntercomDefaultWidget'
    }
  })
}

function anon () {
  anonGa()
  bootIntercom()
}

function signedIn (user) {
  signedInGa(user.details.name)
  bootIntercom(user)
}

function anonGa () {
  if (typeof ga === 'undefined' || ga === null) {
    return
  }
  f_ga('set', 'dimension1', 'no')
  f_ga('set', 'dimension3', 'anonymous')
}

function signedInGa (name) {
  if (typeof ga === 'undefined' || ga === null) {
    return
  }
  f_ga('set', 'dimension1', 'yes')
  f_ga('set', 'dimension3', name)
}

function fetchUser (session, done) {
  return $.ajax({
    type: 'POST',
    url: '/service/',
    data: {
      acrequest: JSON.stringify({
        object: 'AccountManager',
        method: 'getMyInfo',
        params: {
          session_id: session
        }
      })
    },
    success: function (data) {
      if ((data == null) || data.code !== 1) {
        const message = data ? '' : data.response.message
        return done(new Error('Failed request: ' + message))
      }
      return done(null, data.response)
    }
  })
}

function getQueryParameter(key) {
  const items = location.search.substr(1).split('&')
  if (items === '') {
    return null
  }

  for (let i = 0, l = items.length; i < l; ++i) {
    const paramPair = items[i].split('=')
    if (paramPair[0] === key) {
      return decodeURIComponent(paramPair[1])
    }
  }
  return null
}

$(() => {
  $('.open-intercom').on('click', function(e){
    if (window.Intercom) {
      e.preventDefault()
      window.Intercom('showNewMessage')
    }
  })


  const referrer = getQueryParameter('referrer')
  if (referrer && !monster.get('referrer')) {
    const expDate = new Date()
    expDate.setTime(expDate.getTime() + 30 * 86400000)

    monster.set('referrer', referrer, {expires: expDate})
  }

  const session = monster.get('crafted_session')
  if (session == null) return anon()

  fetchUser(session, (error, user) => {
    if (error) return anon()

    // showUser(user)
    signedIn(user)
  })
})
